layui.define(['echarts','echartsTheme'], function(exports){
    var $ = layui.$
        ,layer = layui.layer
        ,echarts = layui.echarts
        ,echartsTheme = layui.echartsTheme;

    const default_colors = {
        Safe:'#1184dc',
        Warning:'rgb(218,146,44)',
        Disasater:'rgb(214,91,75)'
    }

    //外部接口
    var pie_charts = {
        new: function(dom_object, style){
            return echarts.init(dom_object, style);
        },
        //环状进度条型扇形图，带阈值判断
        circle_progress_par: function(options){
            /*
            options = {
                dom: $('#ssss')
                ,stat: 0|1|2   //告警状况
                ,color: {Safe:'XXX',Warning:'XXX', Disaster:'XXX'}
                ,unit:'MB'
                ,text:'内存使用'
                ,value:'15535'
                ,max_value:'65536'
            }
            */ 
            // // 基于准备好的dom，初始化echarts实例
            // var myChart = pie_charts.new(options.dom, echartsTheme);
            var myChart;
            var colorPalette = [];
            switch(options.stat){
                case 0:
                    colorPalette = ['#009688','rgb(225, 239, 211)'];
                    break;
                case 1:
                    colorPalette = ['rgb(204, 180, 56)','rgb(235, 228, 192)'];
                    break;
                case 2:
                    colorPalette = ['rgb(166, 59, 41)','rgb(236, 205, 200)'];
                    break;    
                default:
                    colorPalette = ['#009688','rgb(225, 239, 211)'];
            }
            myChart= pie_charts.new(options.dom, {color: colorPalette});
            // 指定图表的配置项和数据
            var colorObj = options.color|default_colors
         
            //根据数值在定义区间valObj中范围改变，显示colorObj中对应颜色
            function changecolor(stat){
                var color;
                switch(stat){
                    case 0:
                        color = colorObj.Safe;
                    case 1:
                        color = colorObj.Warning;
                    case 2:
                        color = colorObj.Disasater;
                    default:
                        color =colorObj.Disaster;
                }
                return color;
            } 
          
            var option = {
                // backgroundColor: '#2F4056',//背景颜色
                title: {
                    text: `${options.value}`+options.unit,//圆环中间显示的数值单位
                    subtext: options.text,//圆环数值单位下面的描述文字
                    x: 'center',
                    y: 'center',
                    itemGap: 15,//描述文字与上面数值单位的垂直距离
                    textStyle: {
                        fontSize: 16,
                    },
                    subtextStyle: {
                        fontSize: 16,
                    }
                },
                tooltip : {//提示框浮层属性
                    show: false,
                    transitionDuration:0.8,
                    formatter: "{a} - {c}"+options.unit//提示框浮层内容格式器，支持字符串模板和回调函数两种形式
                },
                series: [{
                    name: options.text,
                    type: 'pie',//饼图类型
                    radius: ['36%', '50%'],//饼图的内、外半径
                    hoverAnimation: false,
                    label: {
                        normal: {
                            show: true,
                        }
                    },
                    // itemStyle: {
                    //     color: ['rgb(198, 101, 101)','rgb(184, 173, 64)'],
                    // },
                    data: [{//系列中的数据内容数组
                        name: '已占用'
                        ,value: options.value,
                        // itemStyle: {
                        //     color: function(){
                        //         layer.msg("1111");
                        //         layer.msg("color");
                        //         return changecolor(options.stat);
                        //     }
                        // }
                    }, {
                        name:'未占用'
                        ,value: options.max_value - options.value
                    }
                    ],
                    animationEasingUpdate: 'cubicInOut',
                    animationDurationUpdate: 1000
                }]
            };
            console.log(option);
            myChart.setOption(option);
            console.log(myChart);
            return myChart;
        }
    }


    exports('pie_charts',pie_charts)
});